package com.xudow.app.newui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.umeng.message.PushAgent;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.model.SimpleUser;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.ui.LoginActivity;
import com.xudow.app.ui.task.LogoutTask;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.PrefUtils;

import java.util.HashMap;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 作者：zhouXJ on 2016/4/6 16:48
 * 账号管理
 */
public class AccountNumActivity extends BaseUiActivity implements View.OnClickListener {
    LogoutTask logoutTask;
    @BindView(R.id.account_num_change_pass_btn)
    TextView account_num_change_pass_btn;

    @BindView(R.id.account_num_login_out_btn)
    TextView account_num_login_out_btn;

    @BindView(R.id.user_ico_iv)
    CircleImageView user_ico_iv;//用户头像

    @BindView(R.id.name_tv)
    TextView name_tv;//用户名称

    @BindView(R.id.phone_tv)
    TextView phone_tv;//用户电话

    private UserProfileWithOther profile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_num);
        profile=getMyApp().getProfile();

    }

    @Override
    protected void onStart() {
        super.onStart();
        name_tv.setText(profile.getName());
        phone_tv.setText(profile.getMobilePhone());
        //加载用户头像
        String url = String.format("%s%s?name=%s", Config.BASE_URL, Config.IMAGE_LOAD_URL, profile.getPhotoPath());
        ImageUtils.loadImage(this, user_ico_iv, url, R.mipmap.def_user_ico);
    }

    @Override
    protected void initData() {

    }

    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initListener() {
        account_num_change_pass_btn.setOnClickListener(this);
        account_num_login_out_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.account_num_change_pass_btn: // 修改密码
                Intent intent = new Intent(this, ModifyPwdActivity.class);

                startActivity(intent);
                break;

            case R.id.account_num_login_out_btn: // 退出当前账号
                showLodingDialog(getString(R.string.logout_prompt));
                logoutTask = new LogoutTask(this, logoutTaskHandler);
                logoutTask.execute(new HashMap<String, String>());

                break;
        }

    }
    /**
     * 退出登录处理Handler
     */
    private Handler logoutTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            XApplication application = (XApplication) getApplication();
            application.setCookieStore(null);
            application.setRoles(null);

            final SimpleUser user = PrefUtils.getUserInfo(AccountNumActivity.this);

            //关闭推送
            new Thread(new Runnable() {
                @Override
                public void run() {
                    PushAgent mPushAgent = PushAgent.getInstance(getApplicationContext());
                    try {
                        mPushAgent.removeAlias(user.getUsername(), Config.XUDOW_UMENG_MSG_ALIAS);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            //清除登录用户名
            user.setUsername("");
            user.setPassword("");
            PrefUtils.saveUserInfo(AccountNumActivity.this, user);

            PrefUtils.setPrefLoginStatus(AccountNumActivity.this, false);
            PrefUtils.saveHomeMessageTimeStamp(AccountNumActivity.this, "");

            Intent intent = new Intent(AccountNumActivity.this, PreLoginActivity.class);
            intent.putExtra("GOTOHOME", true);
            startActivity(intent);

           // sendExitBroadcast();

            finish();
        }
    };
}
